home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 October / MacFormat CD Edition MF42 (October 1996).ISO / mac / Shareware City / Developers / FFTs for RISC 1.2 / rfftTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-27  |  3.2 KB  |  137 lines  |  [TEXT/CWIE]

  1. /*  A program to test and time real input fast fourier transform routine    */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include "fftlib.h"
  7.  
  8. #define    MACTIMING 1        /* true means time fft with macintosh calls    */
  9. #if MACTIMING
  10. #include <timer.h>
  11. #endif
  12.  
  13. #define    NUMROWS 1        /* process Matrix of NUMROWS different ffts of length N    */
  14. #define N 2048            /* size of FFT, must be a power of 2 */
  15. #define NTIMES 20        /* number of timing loops, invalid if too big (if a[0][0] == 0 | nan)*/
  16.  
  17. void main(){
  18. float        *Utbl;
  19. float        (*a)[N];
  20.  
  21. #if MACTIMING
  22. UnsignedWide         TheTime1;
  23. UnsignedWide         TheTime2;
  24. UnsignedWide         TheTime3;
  25. double        TheTime;
  26. #endif
  27.  
  28. long         i, il;
  29. long         TheErr;
  30. long        M;
  31.  
  32. Utbl = (float *) malloc((N/4+1)*sizeof(float));
  33. if (Utbl==0)
  34.     TheErr = 2;
  35. else
  36. TheErr = rFFTInit(&M, N, Utbl);
  37.  
  38. if(!TheErr){
  39.     a = (float (*)[N]) malloc(NUMROWS*N*sizeof(float));
  40.     if (a == 0) TheErr = 2;
  41. }
  42.  
  43. if(!TheErr){
  44.  
  45.             /*  set up a simple test case */
  46.     for (il=0; il<NUMROWS; il++){
  47.         for (i=0; i<N; i+=2){
  48.             a[il][i] = sqrt(il+i+.77777);    
  49.             a[il][i+1] = (il+i+.22222)*(il+i+.22222) / N - N/2;    
  50.         }
  51.         a[il][0] = N+3;
  52.         a[il][2] = 1-N;
  53.     }
  54.  
  55. #if MACTIMING
  56.     Microseconds(&TheTime1);
  57.  
  58.     for (i=0;i<NTIMES;i++){        /* do many times for timing */
  59.         rffts((float *)a, M, NUMROWS, Utbl);
  60.     }
  61.  
  62.     Microseconds(&TheTime2);
  63.  
  64.     for (i=0;i<NTIMES;i++){        /* do many times for timing */
  65.         riffts((float *)a, M, NUMROWS, Utbl);
  66.     }
  67.  
  68.     Microseconds(&TheTime3);
  69.  
  70.     TheTime = (double)(TheTime2.hi - TheTime1.hi) * 65536.0 * 65536.0;
  71.     TheTime = (TheTime + (double)(TheTime2.lo - TheTime1.lo))/1000.0;
  72.     printf("Time rfft = %12f   ms.    a[0][0]= %6e\n", TheTime/NTIMES/NUMROWS, a[0][0]);
  73.     TheTime = (double)(TheTime3.hi - TheTime2.hi) * 65536.0 * 65536.0;
  74.     TheTime = (TheTime + (double)(TheTime3.lo - TheTime2.lo))/1000.0;
  75.     printf(" rifft = %12f  ms. a[0][0].Re= %6e\n", TheTime/NTIMES/NUMROWS, a[0][0]);
  76.  
  77. #else
  78.     printf("start timing \n");
  79.     for (i=0;i<NTIMES;i++){        /* do many times for timing */
  80.         rffts((float *)a, M, NUMROWS, Utbl);
  81.         riffts((float *)a, M, NUMROWS, Utbl);
  82.     }
  83.     printf("end timing \n");
  84. #endif
  85.  
  86.     printf("\n");
  87.             /*  set up a simple test case */
  88.     for (il=0; il<NUMROWS; il++){
  89.         for (i=0; i<N; i+=2){
  90.             a[il][i] = sqrt(il+i+.77777);    
  91.             a[il][i+1] = (il+i+.22222)*(il+i+.22222) / N - N/2;    
  92.         }
  93.         a[il][0] = N+3;
  94.         a[il][2] = 1-N;
  95.     }
  96.  
  97.     rffts((float *)a, M, NUMROWS, Utbl);
  98.  
  99.     if (N*NUMROWS <= 512){
  100.          for (il=0; il<NUMROWS; il++){
  101.             printf("atrans = [ \n");
  102.             for (i=0; i<N; i+=2)
  103.                 printf(" %+20.15e + j * ( %+20.15e ) \n", a[il][i], a[il][i+1]);
  104.             printf("]; \n");    
  105.             }
  106.         }
  107.     else {  /* abbreviate big output */
  108.         printf("the last 20 values of the first real fft are: \n");
  109.         for (i=N-40; i<N; i+=2)
  110.             printf(" %+20.15e + j * ( %+20.15e ) \n", a[0][i], a[0][i+1]);
  111.     }
  112.  
  113.     riffts((float *)a, M, NUMROWS, Utbl);
  114.  
  115.     if (N*NUMROWS <= 512){
  116.          for (il=0; il<NUMROWS; il++){
  117.             printf("aitrans = [ \n");
  118.             for (i=0; i<N; i+=2)
  119.                 printf(" %+20.15e ; %+20.15e \n", a[il][i], a[il][i+1]);
  120.             printf("]; \n");    
  121.             }
  122.         }
  123.     else {  /* abbreviate big output */
  124.         printf("the last 20 values of the first real ifft are: \n");
  125.         for (i=N-40; i<N; i+=1)
  126.                 printf(" %+20.15e \n", a[il][i]);
  127.     }
  128.  
  129.     free (a);
  130.     free (Utbl);
  131. }
  132. else
  133. if(TheErr==2)    printf(" out of memory ");
  134. else    printf(" error ");
  135. return;
  136. }
  137.